package com.koron.ticket.line.workChange.ticketchange.service.impl;


import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.common.core.util.CodeTools;
import com.koron.ticket.line.work.workticket.service.WorkTicketService;
import com.koron.ticket.line.workChange.ticketchange.bean.TicketChange;
import com.koron.ticket.line.workChange.ticketchange.bean.TicketChangeQuery;
import com.koron.ticket.line.workChange.ticketchange.enums.TicketChangeType;
import com.koron.ticket.line.workChange.ticketchange.mapper.TicketChangeMapper;
import com.koron.ticket.line.workChange.ticketchange.service.TicketChangeService;
import com.koron.ticket.utils.StringUtils;
import com.koron.ticket.workchange.ticketchangecontent.bean.TicketChangeContent;
import com.koron.ticket.workchange.ticketchangecontent.bean.TicketChangeContentQuery;
import com.koron.ticket.workchange.ticketchangecontent.service.TicketChangeContentService;
import com.koron.ticket.workchange.ticketchangedelay.bean.TicketChangeDelay;
import com.koron.ticket.workchange.ticketchangedelay.bean.TicketChangeDelayQuery;
import com.koron.ticket.workchange.ticketchangedelay.service.TicketChangeDelayService;
import com.koron.ticket.workchange.ticketchangedirector.bean.TicketChangeDirector;
import com.koron.ticket.workchange.ticketchangedirector.bean.TicketChangeDirectorQuery;
import com.koron.ticket.workchange.ticketchangedirector.service.TicketChangeDirectorService;
import com.koron.ticket.workchange.ticketchangeinterrupt.bean.TicketChangeInterrupt;
import com.koron.ticket.workchange.ticketchangeinterrupt.bean.TicketChangeInterruptQuery;
import com.koron.ticket.workchange.ticketchangeinterrupt.service.TicketChangeInterruptService;
import com.koron.ticket.workchange.ticketchangemember.bean.TicketChangeMember;
import com.koron.ticket.workchange.ticketchangemember.bean.TicketChangeMemberQuery;
import com.koron.ticket.workchange.ticketchangemember.service.TicketChangeMemberService;
import com.koron.ticket.workchange.ticketchangesecurityarrangement.bean.TicketChangeSecurityArrangement;
import com.koron.ticket.workchange.ticketchangesecurityarrangement.bean.TicketChangeSecurityArrangementQuery;
import com.koron.ticket.workchange.ticketchangesecurityarrangement.service.TicketChangeSecurityArrangementService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * (TicketChange)表服务实现类
 *
 * @author
 * @since 2021-10-26 14:51:26
 */
@Service("lineTicketChangeService")
public class TicketChangeServiceImpl implements TicketChangeService {

    @Autowired
    WorkTicketService lineWorkTicketService;

    @Autowired
    TicketChangeDirectorService ticketChangeDirectorService;

    @Autowired
    TicketChangeDelayService ticketChangeDelayService;

    @Autowired
    TicketChangeInterruptService ticketChangeInterruptService;

    @Autowired
    TicketChangeContentService ticketChangeContentService;

    @Autowired
    TicketChangeMemberService ticketChangeMemberService;

    @Autowired
    TicketChangeSecurityArrangementService ticketChangeSecurityArrangementService;

    @TaskAnnotation("checkTimeOut")
    public void checkTimeOut(SessionFactory factory, String type, Date updateTime) {
        if (StrUtil.isBlank(type)) {
            return;
        }
        TicketChangeQuery ticketChangeQuery = new TicketChangeQuery();
        ticketChangeQuery.setType(type);
        TicketChangeMapper mapper = factory.getMapper(TicketChangeMapper.class);
        TicketChange ticketchange = mapper.checkTimeOut(ticketChangeQuery);
        if (ticketchange == null || ticketchange.getUpdateTime() == null || updateTime == null) {
            return;
        }
        //是否小于数据库最后更新时间
        long between = DateUtil.between(ticketchange.getUpdateTime(), updateTime, DateUnit.MS, false);
        if (between < 0) {
            Assert.isFalse(true, "信息已过期请刷新界面");
            return;
        }
    }

    /**
     * 新增数据
     */
    @Override
    @TaskAnnotation("insert")
    public Response insert(SessionFactory factory, TicketChange ticketChange) {
        TicketChangeMapper mapper = factory.getMapper(TicketChangeMapper.class);
        checkTimeOut(factory, ticketChange.getType(), ticketChange.getUpdateTime());
        String workTicketId = ticketChange.getWorkTicketId();
        String workTicketCode = ticketChange.getWorkTicketCode();
        String id = CodeTools.getCode32();

        // 1. 设置编号
        String code = mapper.getMaxCode(workTicketId);
        if (StringUtils.isEmpty(code)) {
            code = workTicketCode + "-BG001";
        } else {
            String newCode = code.substring(code.length() - 3);
            Integer suffix = Integer.parseInt(newCode);
            NumberFormat nf = new DecimalFormat("000");
            suffix += 1;
            code = workTicketCode + "-BG" + nf.format(suffix);
        }
        ticketChange.setCode(code);
        // 2. 设置id
        ticketChange.setId(id);

        mapper.insert(ticketChange);
        return Response.success(id);
    }

    /**
     * 根据id删除单条数据
     */
    @Override
    @TaskAnnotation("deleteById")
    public Response deleteById(SessionFactory factory, String id) {
        TicketChangeMapper mapper = factory.getMapper(TicketChangeMapper.class);
        mapper.deleteById(id);
        return Response.success("删除成功");
    }

    /**
     * 根据workTicketId删除下列数据
     * 1. ticket_change
     * 2. ticket_change_content
     * 3. ticket_change_delay
     * 4. ticket_change_director
     * 5. ticket_change_interrupt
     * 6. ticket_change_member
     * 7. ticket_change_security_arrangement
     */
    @Override
    public void deleteAllChangesByWorkTicketId(SessionFactory factory, String workTicketId) {
        // 1. 删除ticket_change
        TicketChangeMapper ticketChangeMapper = factory.getMapper(TicketChangeMapper.class);
        ticketChangeMapper.deleteByWorkTicketId(workTicketId);
        // 2. 删除ticket_change_content
        ticketChangeContentService.deleteByWorkTicketId(factory, workTicketId);
        // 3. 删除ticket_change_delay
        ticketChangeDelayService.deleteByWorkTicketId(factory, workTicketId);
        // 4. 删除ticket_change_director
        ticketChangeDirectorService.deleteByWorkTicketId(factory, workTicketId);
        // 5. 删除ticket_change_interrupt
        ticketChangeInterruptService.deleteByWorkTicketId(factory, workTicketId);
        // 6. 删除ticket_change_member
        ticketChangeMemberService.deleteByWorkTicketId(factory, workTicketId);
        // 7. 删除ticket_change_security_arrangement
        ticketChangeSecurityArrangementService.deleteByWorkTicketId(factory, workTicketId);
    }

    /**
     * 更新数据
     */
    @Override
    @TaskAnnotation("update")
    public Response update(SessionFactory factory, TicketChange ticketChange) {
        TicketChangeMapper mapper = factory.getMapper(TicketChangeMapper.class);
        mapper.update(ticketChange);
        return Response.success("更新成功");
    }

    /**
     * 根据id查询单条数据
     */
    @Override
    @TaskAnnotation("getById")
    public Response getById(SessionFactory factory, String id) {
        TicketChangeMapper mapper = factory.getMapper(TicketChangeMapper.class);

        // 1. 先查出变更单
        TicketChangeQuery ticketChangeQuery = new TicketChangeQuery();
        ticketChangeQuery.setId(id);
        TicketChange ticketChange = mapper.getSingle(ticketChangeQuery);

        // 2. 根据变更类型查出对应的数据
        String type = ticketChange.getType();
        if (type.equals(TicketChangeType.DIRECTOR_CHANGE.getType())) {
            // 2.1 工作负责人变更
            TicketChangeDirectorQuery ticketChangeDirectorQuery = new TicketChangeDirectorQuery();
            ticketChangeDirectorQuery.setChangeId(id);
            TicketChangeDirector ticketChangeDirector = ticketChangeDirectorService.getSingle(factory, ticketChangeDirectorQuery);
            List<TicketChangeDirector> ticketChangeDirectorList = new ArrayList<>();
            ticketChangeDirectorList.add(ticketChangeDirector);
            ticketChange.setTicketChangeDirectorList(ticketChangeDirectorList);
        } else if (type.equals(TicketChangeType.TICKET_DELAY.getType())) {
            // 2.2 工作票延期
            TicketChangeDelayQuery ticketChangeDelayQuery = new TicketChangeDelayQuery();
            ticketChangeDelayQuery.setChangeId(id);
            TicketChangeDelay ticketChangeDelay = ticketChangeDelayService.getSingle(factory, ticketChangeDelayQuery);
            List<TicketChangeDelay> ticketChangeDelayList = new ArrayList<>();
            ticketChangeDelayList.add(ticketChangeDelay);
            ticketChange.setTicketChangeDelayList(ticketChangeDelayList);
        } else if (type.equals(TicketChangeType.WORK_INTERRUPT.getType())) {
            // 2.3 工作间断
            TicketChangeInterruptQuery ticketChangeInterruptQuery = new TicketChangeInterruptQuery();
            ticketChangeInterruptQuery.setChangeId(id);
            TicketChangeInterrupt ticketChangeInterrupt = ticketChangeInterruptService.getSingle(factory, ticketChangeInterruptQuery);
            List<TicketChangeInterrupt> ticketChangeInterruptList = new ArrayList<>();
            ticketChangeInterruptList.add(ticketChangeInterrupt);
            ticketChange.setTicketChangeInterruptList(ticketChangeInterruptList);
        } else if (type.equals(TicketChangeType.ADD_WORK_CONTENT.getType())) {
            // 2.4 增加工作内容
            TicketChangeContentQuery ticketChangeContentQuery = new TicketChangeContentQuery();
            ticketChangeContentQuery.setChangeId(id);
            TicketChangeContent ticketChangeContent = ticketChangeContentService.getSingle(factory, ticketChangeContentQuery);
            List<TicketChangeContent> ticketChangeContentList = new ArrayList<>();
            ticketChangeContentList.add(ticketChangeContent);
            ticketChange.setTicketChangeContentList(ticketChangeContentList);
        } else if (type.equals(TicketChangeType.MEMBER_CHANGE.getType())) {
            // 2.5 工作班组成员变动
            TicketChangeMemberQuery ticketChangeMemberQuery = new TicketChangeMemberQuery();
            ticketChangeMemberQuery.setChangeId(id);
            TicketChangeMember ticketChangeMember = ticketChangeMemberService.getSingle(factory, ticketChangeMemberQuery);
            List<TicketChangeMember> ticketChangeMemberList = new ArrayList<>();
            ticketChangeMemberList.add(ticketChangeMember);
            ticketChange.setTicketChangeMemberList(ticketChangeMemberList);
        } else if (type.equals(TicketChangeType.SECURITY_ARRANGEMENT_CHANGE.getType())) {
            // 2.6 部分安全措施变动
            TicketChangeSecurityArrangementQuery ticketChangeSecurityArrangementQuery = new TicketChangeSecurityArrangementQuery();
            ticketChangeSecurityArrangementQuery.setChangeId(id);
            TicketChangeSecurityArrangement ticketChangeSecurityArrangement = ticketChangeSecurityArrangementService.getSingle(factory, ticketChangeSecurityArrangementQuery);
            List<TicketChangeSecurityArrangement> ticketChangeSecurityArrangementList = new ArrayList<>();
            ticketChangeSecurityArrangementList.add(ticketChangeSecurityArrangement);
            ticketChange.setTicketChangeSecurityArrangementList(ticketChangeSecurityArrangementList);
        } else {
            return Response.fail("变更类型有问题");
        }

        return Response.success(ticketChange);
    }

    /**
     * 分页查询数据
     */
    @Override
    @TaskAnnotation("getListAndPaging")
    public Response getListAndPaging(SessionFactory factory, TicketChangeQuery ticketChangeQuery) {
        TicketChangeMapper mapper = factory.getMapper(TicketChangeMapper.class);
        List<TicketChange> ticketChangeList = mapper.getListAndPaging(ticketChangeQuery);
        for (TicketChange ticketChange : ticketChangeList) {
            String workTicketId = ticketChange.getWorkTicketId();
            String workTicketType = lineWorkTicketService.getTypeById(factory, workTicketId);
            ticketChange.setWorkTicketType(workTicketType);
        }
        Integer count = mapper.count(ticketChangeQuery);
        return Response.success("查询成功", new PageInfo(ticketChangeList), count);
    }

    @Override
    @TaskAnnotation("getByWorkTicketId")
    public TicketChange getByWorkTicketId(SessionFactory factory, String workTicketId) {
        TicketChange ticketChange = new TicketChange();

        // 1. 查出所有的工作负责人变更
        TicketChangeDirectorQuery ticketChangeDirectorQuery = new TicketChangeDirectorQuery();
        ticketChangeDirectorQuery.setWorkTicketId(workTicketId);
        List<TicketChangeDirector> ticketChangeDirectorList = ticketChangeDirectorService.getList(factory, ticketChangeDirectorQuery);
        ticketChange.setTicketChangeDirectorList(ticketChangeDirectorList);
        // 2. 查出所有的工作票延期
        TicketChangeDelayQuery ticketChangeDelayQuery = new TicketChangeDelayQuery();
        ticketChangeDelayQuery.setWorkTicketId(workTicketId);
        List<TicketChangeDelay> ticketChangeDelayList = ticketChangeDelayService.getList(factory, ticketChangeDelayQuery);
        ticketChange.setTicketChangeDelayList(ticketChangeDelayList);
        // 3. 查出所有的工作间断
        TicketChangeInterruptQuery ticketChangeInterruptQuery = new TicketChangeInterruptQuery();
        ticketChangeInterruptQuery.setWorkTicketId(workTicketId);
        List<TicketChangeInterrupt> ticketChangeInterruptList = ticketChangeInterruptService.getList(factory, ticketChangeInterruptQuery);
        ticketChange.setTicketChangeInterruptList(ticketChangeInterruptList);
        // 4. 查看所有的增加工作内容
        TicketChangeContentQuery ticketChangeContentQuery = new TicketChangeContentQuery();
        ticketChangeContentQuery.setWorkTicketId(workTicketId);
        List<TicketChangeContent> ticketChangeContentList = ticketChangeContentService.getList(factory, ticketChangeContentQuery);
        ticketChange.setTicketChangeContentList(ticketChangeContentList);
        // 5. 查看所有的工作班组成员变动
        TicketChangeMemberQuery ticketChangeMemberQuery = new TicketChangeMemberQuery();
        ticketChangeMemberQuery.setWorkTicketId(workTicketId);
        List<TicketChangeMember> ticketChangeMemberList = ticketChangeMemberService.getList(factory, ticketChangeMemberQuery);
        ticketChange.setTicketChangeMemberList(ticketChangeMemberList);
        // 6. 查看所有的部分安全措施变动
        TicketChangeSecurityArrangementQuery ticketChangeSecurityArrangementQuery = new TicketChangeSecurityArrangementQuery();
        ticketChangeSecurityArrangementQuery.setWorkTicketId(workTicketId);
        List<TicketChangeSecurityArrangement> ticketChangeSecurityArrangementList = ticketChangeSecurityArrangementService.getList(factory, ticketChangeSecurityArrangementQuery);
        ticketChange.setTicketChangeSecurityArrangementList(ticketChangeSecurityArrangementList);

        return ticketChange;
    }
}
